﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Collections;
using System.Windows.Forms;

namespace iAddin
{
    class VisualStudioInstallInfo
    {
        #region Variables
        private string csProjItemPath;

        public string CSProjItemPath
        {
            get { return csProjItemPath; }
            set { csProjItemPath = value; }
        }
        private string csProjectPath;

        public string CSProjectPath
        {
            get { return csProjectPath; }
            set { csProjectPath = value; }
        }
        private string cppProjectPath;

        public string CPPProjectPath
        {
            get { return cppProjectPath; }
            set { cppProjectPath = value; }
        }
        private string cppProjItemPath;

        public string CPPProjItemPath
        {
            get { return cppProjItemPath; }
            set { cppProjItemPath = value; }
        }
        #endregion

        /// <summary>
        /// Retrieve the locations to where Visual Studio stores its templates
        /// </summary>
        public bool GetVSPaths(string sVisualStudioVersion)
        {
            RegistryKey regKey = null;
            try
            {
                regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE")
                    .OpenSubKey("Microsoft")
                    .OpenSubKey("VisualStudio")
                    .OpenSubKey(sVisualStudioVersion)
                    .OpenSubKey("Projects");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            if (regKey == null)
                return false;

            // C#
            RegistryKey itemRegKey = regKey.OpenSubKey("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}");
            CSProjItemPath = (string)itemRegKey.GetValue(@"ItemTemplatesDir");
            
            itemRegKey = regKey.OpenSubKey("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}");
            CSProjectPath = (string)itemRegKey.GetValue(@"ProjectTemplatesDir");

            // C++
            itemRegKey = regKey.OpenSubKey("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}");
            CPPProjItemPath = (string)itemRegKey.GetValue(@"ItemTemplatesDir");

            itemRegKey = regKey.OpenSubKey("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}");
            CPPProjItemPath = (string)itemRegKey.GetValue(@"ProjectTemplatesDir");
            return true;
        }
    }
    class iInstallWizard
    {
        #region Variables
        private const string iWazardRegistryKey = "iAddin_Project Manager";
        private const string addinName = "iAddin_Project Manager";
        private ArrayList visualStudioVersion;

        public ArrayList VisualStudioVersion
        {
            get { return visualStudioVersion; }
        }
        #endregion

        iInstallWizard()
        {
            visualStudioVersion = new ArrayList();
            visualStudioVersion.Add("8.0");
            visualStudioVersion.Add("9.0");
        }

        public void CopyWizardFile(String selectVisualStudioVersion)
        {
            RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey(addinName).OpenSubKey(iWazardRegistryKey);
            string sInstallDir = (string)rk.GetValue(@"InstallDir");

            VisualStudioInstallInfo vsInstallInfo = new VisualStudioInstallInfo();

            if (selectVisualStudioVersion != null)
            {
                if (!vsInstallInfo.GetVSPaths(vsVersion))
                    continue;
                CopyWizardFile(sInstallDir, vsInstallInfo);
            }
            else
            {
                foreach (String vsVersion in VisualStudioVersion)
                {
                    if (!vsInstallInfo.GetVSPaths(vsVersion))
                        continue;
                    CopyWizardFile(sInstallDir, vsInstallInfo);
                }
            }            
        }

        private static void CopyWizardFile(string sInstallDir, VisualStudioInstallInfo vsInstallInfo)
        {
            string sSourceDir, sDestDir;
            // C++
            sSourceDir = Path.Combine(sInstallDir, @"VSZ files\CSharp\Project Items\Krevera Template Manager");
            sDestDir = Path.Combine(vsInstallInfo.CSProjItemPath, @"Krevera Template Manager");
            iSystem.iDirectory.CopyDirectoryFile(sSourceDir, sDestDir, true);

            sSourceDir = Path.Combine(sInstallDir, @"VSZ files\VC++\Project Items\Krevera Template Manager");
            sDestDir = Path.Combine(vsInstallInfo.CSProjectPath, @"Krevera Template Manager");
            iSystem.iDirectory.CopyDirectoryFile(sSourceDir, sDestDir, true);
        }
    }
}
